<template>
    <div class="container">
        <Vcode class="cover" v-if="verifyCode" :show="verifyCode" @success="success" @close="close" />
        <h1 style="color:#fff;font-size:60px;font-weight:bold;margin:100px 0 50px 0;">欢迎登录后台</h1>
        <el-form :model="form" :rules="rules" ref="form_ref" class="form-container">
            <el-form-item prop="username">
                <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" v-model="form.username"
                    class="form-item-width" clearable />
            </el-form-item>
            <el-form-item prop="password">
                <el-input prefix-icon="el-icon-key" placeholder="请输入密码" v-model="form.password" class="form-item-width"
                    clearable show-password />
            </el-form-item>
            <el-button type="primary" class="form-item-width" @click="login">登录</el-button>
        </el-form>
    </div>
</template>

<script>
import Vcode from "vue-puzzle-vcode";

export default {
    name: "Login",
    data() {
        return {
            form: {},
            verifyCode: false,
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 10, message: '长度在6到10位', trigger: 'blur' }
                ],
            }
        }
    },
    components: {
        Vcode
    },
    methods: {
        login() {
            this.$refs["form_ref"].validate((valid) => {
                if (valid) {
                    this.verifyCode = true;
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        success() {
            this.request.post("/api/admin/login", this.form).then(res => {
                // console.log(res);
                if (res.code === '200') {
                    this.$notify.success('登录成功');
                    if (res.data != null) {
                        sessionStorage.setItem('admin', JSON.stringify(res.data));
                        console.log(res.data);
                    }

                    this.$router.push("/layout");
                } else {
                    this.$notify.error(res.msg);
                    this.verifyCode = false;
                }
            })
        },
        close(){

        }
    }
}
</script>

<style lang="scss" scoped>
.container {
    width: 100vw;
    height: 100vh;

    background: url("../../assets/images/bkg.png");
    background-size: cover;

    display: flex;
    flex-direction: column;
    align-items: center;
}

.cover {
    width: fit-content;
    background-color: #ffffff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1000;
}

.form-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    width: 500px;
    height: 400px;

    background-color: rgba(255, 255, 255, .5);
    border-radius: 25px;



    .form-item-width {
        width: 390px !important;
    }
}
</style>